package de.slothsoft.commons.swing;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;

import javax.swing.JFrame;


public abstract class AbstractMainFrame extends JFrame {

    private static final long serialVersionUID = 1L;

    public void open() {
	createMainPanel();
	internalSetVisible(true);
    }

    /**
     * Use open() instead.
     */

    @Override
    @Deprecated
    public void setVisible(boolean b) {
	if (b) {
	    open();
	} else {
	    internalSetVisible(b);
	}
    }

    protected void internalSetVisible(boolean b) {
	super.setVisible(b);
    }

    private void createMainPanel() {
	setLayout(new BorderLayout());

	Component component = createComponents();
	if (component != null) {
	    add(component, BorderLayout.CENTER);
	}

	Dimension size = getInitialSize();
	if (size != null) {
	    setSize(size);
	}
	setDefaultCloseOperation(EXIT_ON_CLOSE);
	WindowUtil.centerWindow(this);
    }

    protected boolean hasAboutToolbar() {
	return false; // to be overridden
    }

    protected boolean hasToolbar() {
	return false; // to be overridden
    }

    // must be overriden

    protected abstract Component createComponents();

    public Dimension getInitialSize() {
	return new Dimension(800, 600);
    }
}
